import cgi
import os
from google.appengine.ext.webapp import template
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class Contacto(db.Model):
  usuario = db.UserProperty()
  nombre = db.StringProperty()
  apellido = db.StringProperty()
  fechaNac = db.StringProperty()
  mail = db.StringProperty()
  cel= db.StringProperty()
  esHombre=db.BooleanProperty()
  date = db.DateTimeProperty(auto_now_add=True)
  
  
class MainPage(webapp.RequestHandler):
  def get(self):
     
     contactos=db.GqlQuery("SELECT * FROM Contacto WHERE usuario = :1",
                          users.get_current_user())

     template_values = {
       'contactos': contactos,
	   'salir_url': users.create_logout_url(self.request.uri), 
       }

     path = os.path.join(os.path.dirname(__file__), 'index.htm')
     self.response.out.write(template.render(path, template_values))
	   

class ContactList(webapp.RequestHandler):
  def post(self):
    contacto = Contacto()

    if users.get_current_user():
      contacto.usuario = users.get_current_user()

    contacto.cel=self.request.get('txtCel')
    contacto.fechaNac=self.request.get('selDia') + "/" + self.request.get('selMes') + "/" + self.request.get('selAnio')
    contacto.mail=self.request.get('txtMail')
    contacto.nombre=self.request.get('txtNombre')
    contacto.sexo=self.request.get('selSexo')
    contacto.apellido=self.request.get('txtApellido')
	
    if self.request.get('selSexo')=='M':
      contacto.esHombre=True
    else:
      contacto.esHombre=False
	  
	  
    contacto.put()
    self.redirect('/')

application = webapp.WSGIApplication(
                                     [('/', MainPage),
                                      ('/new', ContactList),],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()